#ifndef _C_BACKUP_H_
#define _C_BACKUP_H_

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>

#include <boost/circular_buffer.hpp>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>


class CBackup {
 public:
    typedef boost::circular_buffer<IplImage*> ImageBuffer_t;
    
    boost::circular_buffer<ImageBuffer_t> imgsbuffer;
    boost::circular_buffer<CvSize> sizesbuffer;

    int buffer_to_store;
    int buffer_to_show;
    int m_nbuffers;
    int m_sizebuffer;
    FILE * videofile;

 public:
    CBackup(int sizebuffer)
    {
	m_sizebuffer = sizebuffer;
	buffer_to_show = buffer_to_store = 0;
	videofile = 0;
    }

    CBackup(int sizebuffer, int nimgs, CvSize size1, ...);
    void alloc(int nimgs, CvSize size1, ...);

    ~CBackup()
    {
	if (videofile)
	    fclose(videofile);
	int i, j;
	for (j=0; j<m_nbuffers; j++)
	    for (i=0; i<m_sizebuffer; i++)
		cvReleaseImage(&(imgsbuffer[j][i]));
    }

    IplImage * img(unsigned int n);
    IplImage * operator [] (unsigned int n);
    void copy_backup(IplImage **img1, ...);
    void copy_backup();
    void rewind();
    int back_one();
    int forward_one();
    int save_init(std::string path, std::string name, CvSize image_size);
    int saveframe();
};


#endif
